<template>
  <el-table :data="tableData" class="table" @selection-change="handleSelectionChange">
    <el-table-column type="selection" width="55" />
    <el-table-column label="标题内容" prop="title" />
    <el-table-column label="类型" prop="type" />
    <el-table-column label="时间" prop="adTime" />
    <el-table-column align="right">
      <template #header>
        <el-input
          v-model="search"
          size="large"
          placeholder="搜索相关消息"
          @input="fetchData"
        />
      </template>
      <template #default="scope">
        <el-button size="small" @click="handleEdit(scope.$index, scope.row)">
          查看
        </el-button>
        <el-button
          size="small"
          type="danger"
          @click="handleDelete(scope.$index, scope.row)"
        >
          删除
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  <div>
    <el-row class="pagination">
      <el-button mb-2 @click="toggle" class="tranlate">Switch Language</el-button>
      <el-button mb-2 type="danger" @click="handleBatchDelete">批量删除</el-button>
      <br />
      <el-config-provider :locale="locale">
        <el-col span="8" :offset="8">
          <el-pagination
            :current-page="currentPage"
            :page-size="pageSize"
            :total="total"
            @current-change="handlePageChange"
            background
            layout="prev, pager, next, jumper"
          />
        </el-col>
      </el-config-provider>
    </el-row>
  </div>
</template>

<script lang="ts" setup>
import { computed, ref, onMounted } from "vue";
import axios from "axios";
import zhCn from "element-plus/dist/locale/zh-cn.mjs";
import en from "element-plus/dist/locale/en.mjs";

interface User {
  id: number;
  title: string;
  type: string;
  adTime: string;
}

const language = ref("zh-cn");
const locale = computed(() => (language.value === "zh-cn" ? zhCn : en));
const search = ref("");

const tableData = ref<User[]>([]);
const selectedRows = ref<User[]>([]);
const currentPage = ref(1);
const pageSize = ref(10); // 每页显示10条数据
const total = ref(0);

const handleEdit = (index: number, row: User) => {
  console.log(index, row);
};

const handleDelete = async (index: number, row: User) => {
  try {
    await axios.post("http://localhost:8080/user/noticeDelete", { ids: [row.id] });
    tableData.value.splice(index, 1); // 删除前端数据
    console.log(`Deleted row with id: ${row.id}`);
  } catch (error) {
    console.error("Failed to delete data:", error);
  }
};

const handleSelectionChange = (selection: User[]) => {
  selectedRows.value = selection;
};

const handleBatchDelete = async () => {
  const ids = selectedRows.value.map((row) => row.id);
  if (ids.length === 0) {
    alert("请选择要删除的通知");
    return;
  }
  try {
    await axios.post("http://localhost:8080/user/noticeDelete", { ids });
    tableData.value = tableData.value.filter((row) => !ids.includes(row.id)); // 更新前端数据
    console.log(`Deleted rows with ids: ${ids}`);
  } catch (error) {
    console.error("Failed to delete data:", error);
  }
};

const toggle = () => {
  language.value = language.value === "zh-cn" ? "en" : "zh-cn";
};

// 从后端获取数据
const fetchData = async () => {
  try {
    const response = await axios.get("http://localhost:8080/user/noticeList", {
      params: {
        page: currentPage.value,
        size: pageSize.value,
        search: search.value,
      },
    });
    tableData.value = response.data.data.records;
    total.value = response.data.data.total;
  } catch (error) {
    console.error("Failed to fetch data:", error);
  }
};

const handlePageChange = (page: number) => {
  currentPage.value = page;
  fetchData();
};

onMounted(() => {
  fetchData();
});
</script>

<style scoped>
.table {
  width: 85%;
  margin-left: 10px;
  margin-top: 20px;
}
.pagination {
  margin-top: 20px;
  margin-left: 400px;
}
.tranlate {
  display: none;
}
</style>
